package ge.engine;

import java.util.ArrayList;

/**
 * 
 * @author Abel
 * Mechanic defines the mechanic o rules which establishes the
 * rules of the level.  
 * The mechanic is update befor the components. The order you ask
 * form rules is important, be sure of controll the player rules
 * as the first one.
 */
public class Mechanic implements Rule
{
	ArrayList<Rule> rules;
	ArrayList<Rule> rulesToRemove;
	ArrayList<Rule> rulesToAdd;
	
	public Mechanic()
	{
		rules = new ArrayList<Rule>();
		rulesToRemove = new ArrayList<Rule>();
		rulesToAdd = new ArrayList<Rule>();
	}
	/**
	 * Updates all the levels
	 */
	public void update()
	{
		if(rulesToAdd.size() > 0)
		{
			for(Rule r: rulesToAdd)
			{
				rules.add(r);
			}
			rulesToAdd.clear();
		}
		for(Rule r : rules )
		{
				r.update();
		}
		if(rulesToRemove.size() > 0)
		{
			for(Rule r: rulesToRemove)
			{
				rules.remove(r);
			}
			rulesToRemove.clear();
		}
	}
	
	 
	/**
	 * 
	 * @param r Adds a new rule
	 */
	public void add(Rule r)
	{
		rulesToAdd.add(r);
	}
	/**
	 * 
	 * @param r Removes that rules from rule's arraylist
	 */
	public void remove(Rule r)
	{
		
		rulesToRemove.add(r);
	}
	/**
	 * 	Sets a rule in a concrete position. Be sure that position exists.
	 * @param r Rule
	 * @param index new position
	 */
	public void set(Rule r, int index)
	{
		rules.set(index, r);
	}
	
	/**
	 * 	That function is called just before the mechanic is loaded
	 */
	public void load()
	{
		
	}
	
	/**
	 * 	That function is called just before new mechanic will be loaded
	 */
	public void unload()
	{
		
	}
}
